<?php


namespace App\Model;


class ReceiptCode extends Base
{

    protected $table='receipt_code';

    protected $fillable=['order_id','code','user_id','get_time'];


    /**
     * 生成取货码
     * Create by Peter
     * 2020/06/22 14:35:09
     * Email:904801074@qq.com
     * @param $order_id
     * @param $user_id
     * @return mixed
     */
    static function createCode($order_id,$user_id){


        $code=mt_rand(1000,9999).$order_id;

        $find=ReceiptCode::where('code',$code)->first();

        //取货码重复重新生成
        if($find) return self::createCode($order_id,$user_id);

        ReceiptCode::create([
            'order_id'=>$order_id,
            'code'=>mt_rand(1000,9999).$order_id,
            'user_id'=>$user_id,
        ]);

    }


    function order(){

        return $this->belongsTo(Order::class,'order_id','id');
    }


}
